package org.example.lostandfound.dao;

import org.example.lostandfound.model.LostItem;
import org.example.lostandfound.model.LostItemType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
@Repository
//这个类负责执行与丢失物品相关的所有数据库操作
public class LostItemDaoImpl implements LostItemDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public List<LostItem> findAllLostItems() {
        //查询所有丢失物品的SQL语句
        return jdbcTemplate.query("SELECT * FROM lost_items",new LostItemRowMapper());
    }

    @Override
    public LostItem findLostItemById(Long id) {
        //根据ID查询丢失物品的SQL语句
        return jdbcTemplate.queryForObject("SELECT * FROM lost_items WHERE id=?",new LostItemRowMapper(),id);
    }

    @Override
    public LostItem findLostItemByName(String lostItemName) {
        //根据name查询丢失物品的SQL语句
        return jdbcTemplate.queryForObject("SELECT * FROM lost_items WHERE name=?",new LostItemRowMapper(),lostItemName);
    }

    @Override
    public void insertLostItem(LostItem lostItem) {
        //插入新的丢失物品的SQL的语句
        jdbcTemplate.update("INSERT INTO lost_items(item_name,description,item_type,found_date,found) VALUES (?,?,?,?,?) ",
                lostItem.getLostItemName(),lostItem.getLostItemDescription(),lostItem.getLostItemType(),lostItem.getLostItemDate(),lostItem.isFound());

    }

    @Override
    public void updateLostItem(LostItem lostItem) {
        //更新丢失物品信息的SQL的语句
        jdbcTemplate.update("UPDATE lost_items SET item_name=?, description=?, item_type=?, found_date=?,found=? WHERE id=?",
                lostItem.getLostItemName(),lostItem.getLostItemDescription(),lostItem.getLostItemType(),lostItem.getLostItemDate(),lostItem.isFound());

    }

    @Override
    public void deleteLostItem(Long id) {
        //删除丢失物品的SQL语句
        jdbcTemplate.update("DELETE FROM lost_items WHERE id=?",id);

    }

    @Override
    public List<LostItem> findLostItemsByFound(boolean found) {
        return jdbcTemplate.query("SELECT * FROM lost_items WHERE found=?",new LostItemRowMapper(),found);
    }

    @Override
    public List<LostItem> findLostItemsByType(LostItemType type) {
        return jdbcTemplate.query("SELECT * FROM lost_items WHERE type=?",new LostItemRowMapper(),type.name());
    }

    private class LostItemRowMapper implements RowMapper<LostItem> {
        @Override
        public LostItem mapRow(ResultSet rs, int rowNum) throws SQLException {
            LostItem lostItem = new LostItem();
            lostItem.setLostItemId(rs.getLong("lost_item_id"));
            lostItem.setLostItemName(rs.getString("lost_item_name"));
            lostItem.setLostItemDescription(rs.getString("lost_item_description"));
            lostItem.setLostItemType(LostItemType.valueOf(rs.getString("lost_item_type")));
            lostItem.setLostItemDate(rs.getDate("lost_item_date"));
            lostItem.setFound(rs.getBoolean("found"));
            return lostItem;
        }
    }
}
